package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.CollectionsExhibitMapper;
import com.shaobufan.museum.pojo.entity.CollectionsExhibit;
import com.shaobufan.museum.pojo.vo.CollectionsVo;
import com.shaobufan.museum.service.CollectionsExhibitService;
import com.shaobufan.museum.service.CollectionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * @description 针对表【collections_exhibit(藏品-展览 中间表)】的数据库操作Service实现
 * @createDate 2024-04-07 11:09:50
 */
@Service
public class CollectionsExhibitServiceImpl extends ServiceImpl<CollectionsExhibitMapper, CollectionsExhibit>
        implements CollectionsExhibitService {

    @Resource
    CollectionsExhibitMapper collectionsExhibitMapper;
    @Resource
    CollectionsService collectionsService;

    /**
     * 根据展览 id查询展出的藏品
     *
     * @param exId 展览 id
     * @return List<CollectionsVo>
     */
    @Override
    public List<CollectionsVo> searchCollectionsExhibit(Integer exId) {
        QueryWrapper<CollectionsExhibit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ex_id", exId);

        return getCollectionsVoList(collectionsExhibitMapper.selectList(queryWrapper));
    }

    /**
     * 添加 出展藏品
     *
     * @param exId         展览 id
     * @param collectionId 藏品 id
     * @return Boolean
     */
    @Override
    public Boolean addCollectionsExhibit(Integer exId, Integer collectionId) {
        QueryWrapper<CollectionsExhibit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ex_id", exId);
        queryWrapper.eq("collection_id", collectionId);

        CollectionsExhibit checkCollectionsExhibit = collectionsExhibitMapper.selectOne(queryWrapper);
        if (checkCollectionsExhibit != null) {
            return false;
        }

        CollectionsExhibit collectionsExhibit = new CollectionsExhibit();
        collectionsExhibit.setExId(exId);
        collectionsExhibit.setCollectionId(collectionId);

        int inserted = collectionsExhibitMapper.insert(collectionsExhibit);
        return inserted == 1;
    }

    /**
     * 删除 出展藏品
     *
     * @param exId         展览 id
     * @param collectionId 藏品 id
     * @return Boolean
     */
    @Override
    public Boolean removeCollectionsExhibit(Integer exId, Integer collectionId) {
        QueryWrapper<CollectionsExhibit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ex_id", exId);
        queryWrapper.eq("collection_id", collectionId);

        int deleted = collectionsExhibitMapper.delete(queryWrapper);
        return deleted == 1;
    }

    public List<CollectionsVo> getCollectionsVoList(List<CollectionsExhibit> collectionsExhibitList) {
        return collectionsExhibitList.stream().map(this::getCollectionsVo).collect(Collectors.toList());
    }

    public CollectionsVo getCollectionsVo(CollectionsExhibit collectionsExhibit) {
        Integer collectionId = collectionsExhibit.getCollectionId();
        return collectionsService.searchMuseumCollectionsById(collectionId);
    }
}